﻿// HSS.Interlink.ResourceMgr.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       ResourceMgr.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/22/2011
// ----------------------------------------------------------------------------
using System;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.IO;
using System.Windows.Resources;
using System.Windows;

namespace HSS.Interlink
{
	internal static class ResourceMgr
	{
		/// <summary>
		/// An Image of a Folder.
		/// </summary>
		public static BitmapImage FolderImage
		{
			get
			{
				//return new BitmapImage(new Uri("/HSS.Interlink;component/assets/folder.png", UriKind.Relative));
				return GetBitmap("assets/folder.png");
			}
		}
		/// <summary>
		/// An Image of an Upload File.
		/// </summary>
		public static BitmapImage UploadFileImage
		{
			get
			{
				//return new BitmapImage(new Uri("/HSS.Interlink;component/assets/upload.png", UriKind.Relative));
				return GetBitmap("assets/upload.png");
			}
		}
		/// <summary>
		/// An Image of an Cancel Button.
		/// </summary>
		public static BitmapImage CancelImage
		{
			get
			{
				//return new BitmapImage(new Uri("/HSS.Interlink;component/assets/cancel.png", UriKind.Relative));
				return GetBitmap("assets/cancel.png");
			}
		}
		/// <summary>
		/// An Image of an Information button.
		/// </summary>
		public static BitmapImage InformationImage
		{
			get
			{
				//return new BitmapImage(new Uri("/HSS.Interlink;component/assets/information.png", UriKind.Relative));
				return GetBitmap("assets/information.png");
			}
		}
		/// <summary>
		/// An Image of an Remove File button.
		/// </summary>
		public static BitmapImage RemoveImage
		{
			get
			{
				//return new BitmapImage(new Uri("/HSS.Interlink;component/assets/remove.png", UriKind.Relative));
				return GetBitmap("assets/remove.png");
			}
		}
		/// <summary>
		/// An Image of a Globe.
		/// </summary>
		public static BitmapImage GlobeImage
		{
			get
			{
				return GetBitmap("assets/globe.png");
			}
		}
		/// <summary>
		/// An Image of the Interlink Icon.
		/// </summary>
		public static BitmapImage InterlinkIcon
		{
			get
			{
				return GetBitmap("assets/interlink.png");
			}
		}
		/// <summary>
		/// Gets the simple name of the currently executing Assembly
		/// </summary>
		public static string ExecutingAssemblyName
		{
			get
			{
				string asmFullName = typeof(ResourceMgr).Assembly.FullName;
				return new AssemblyName(asmFullName).Name;
			}
		}

		/// <summary>
		/// Gets the simple name of the Assembly from the specified <see cref="Type"/>
		/// </summary>
		public static string GetAssemblyName(Type type)
		{
			string asmFullName = type.Assembly.FullName;
			return new AssemblyName(asmFullName).Name;
		}
		/// <summary>
		/// Return a Resource Stream
		/// </summary>
		/// <param name="relativeUri">The Uri to the resource</param>
		/// <param name="assemblyName">The name of the assembly where the resource is located</param>
		/// <returns>A Resource Stream</returns>
		public static Stream GetStream(string relativeUri, string assemblyName)
		{
			StreamResourceInfo res = Application.GetResourceStream(new Uri(assemblyName + ";component/" + relativeUri, UriKind.Relative));
			if (res == null)
				res = Application.GetResourceStream(new Uri(relativeUri, UriKind.Relative));

			if (res != null)
				return res.Stream;
			else
				return null;
		}
		/// <summary>
		/// Return a Resource Stream
		/// </summary>
		/// <param name="relativeUri">The Uri to the resource</param>
		/// <returns>A Resource Stream</returns>
		public static Stream GetStream(string relativeUri)
		{
			return GetStream(relativeUri, ExecutingAssemblyName);
		}
		/// <summary>
		/// Return a Resource Bitmap from the local type
		/// </summary>
		/// <param name="relativeUri">The Uri to the resource</param>
		/// <param name="type">The specified <see cref="Type"/> to get the simple name of the Assembly that contains the Image.</param>
		/// <returns>A Resource Bitmap</returns>
		public static BitmapImage GetBitmap(string relativeUri, Type type)
		{
			string asmName = GetAssemblyName(type);
			Stream s = GetStream(relativeUri, asmName);
			if (s == null) return null;
			using (s)
			{
				BitmapImage bmp = new BitmapImage();
				bmp.SetSource(s);
				return bmp;
			}
		}
		/// <summary>
		/// Return a Resource Bitmap
		/// </summary>
		/// <param name="relativeUri">The Uri to the resource</param>
		/// <param name="assemblyName">The name of the assembly where the resource is located</param>
		/// <returns>A Resource Bitmap</returns>
		public static BitmapImage GetBitmap(string relativeUri, string assemblyName)
		{
			Stream s = GetStream(relativeUri, assemblyName);
			if (s == null) return null;
			using (s)
			{
				BitmapImage bmp = new BitmapImage();
				bmp.SetSource(s);
				return bmp;
			}
		}
		/// <summary>
		/// Return a Resource Bitmap
		/// </summary>
		/// <param name="relativeUri">The Uri to the resource</param>
		/// <returns>A Resource Bitmap</returns>
		public static BitmapImage GetBitmap(string relativeUri)
		{
			return GetBitmap(relativeUri, ExecutingAssemblyName);
		}
	}
}